<?php

namespace app\models;

use Yii;
use yii\db\Expression;
/**
 * This is the model class for table "member_activity".
 *
 * @property integer $id
 * @property integer $member_id
 * @property integer $type
 * @property string $dt
 * @property string $ip
 * @property string $activity_info
 *
 * @property Member $member
 */
class MemberActivity extends \yii\db\ActiveRecord
{
    const  ACTIVITY_REG_USER=1;         //        '1'=>'注册',
    const  ACTIVITY_LOGIN=2;            //        '2'=>'登录',
    const  ACTIVITY_LOGOUT=3;           //        '3'=>'登出',
    const  ACTIVITY_CHARGE=4;           //        '4'=>'充值',
    const  ACTIVITY_CASH=5;             //        '5'=>'提现',
    const  ACTIVITY_USD_TO_NEA=6;       //        '6'=>'转账USD到NEA',
    const  ACTIVITY_NEA_TO_USD=7;       //        '7'=>'转账NEA到USD',
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'member_activity';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['member_id', 'type', 'ip', 'activity_info'], 'required'],
            [['member_id', 'type'], 'integer'],
            [['dt'], 'safe'],
            [['ip'], 'string', 'max' => 50],
            [['activity_info'], 'string', 'max' => 128],
            [['member_id'], 'exist', 'skipOnError' => true, 'targetClass' => Member::className(), 'targetAttribute' => ['member_id' => 'id']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'member_id' => '会员ID',
            'type' => '活动',
            'dt' => '日期时间',
            'ip' => 'IP',
            'activity_info' => '活动细节',
        ];
    }

    public function attributeValues()
    {
      return [
        'type'=>[
          '1'=>'注册',
          '2'=>'登录',
          '3'=>'登出',
          '4'=>'充值',
          '5'=>'提现',
          '6'=>'转账USD到NEA',
          '7'=>'转账NEA到USD',
        ],
      ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getMember()
    {
        return $this->hasOne(Member::className(), ['id' => 'member_id']);
    }

    public function addActivity($memberid,$type,$detail){
      $activity = new MemberActivity();
      $activity->member_id=$memberid;
      $activity->type=$type;
      $activity->dt=new Expression('NOW()');
      $activity->ip=Yii::$app->request->getUserIP();
      $activity->activity_info=$detail;
      if(!$activity->save()){
        Yii::getLogger()->log(print_r($activity->errors,true),yii\log\Logger::LEVEL_ERROR,'MyLog');
        return false;
      }
      return true;
    }
}
